﻿layui.use(['form', 'element', 'laydate'], function () {
    var form = layui.form
        , $ = layui.jquery
        , element = layui.element
        , laydate = layui.laydate;

    //日期时间选择器
    laydate.render({
        elem: '#sDepartTime'
        , type: 'datetime',
        format: 'yyyy-MM-dd HH:mm'
    });

    //AddTravel监听提交
    form.on('submit(AddBook)', function (data) {
        var index = parent.layer.getFrameIndex(window.name);
        $.ajax({
            type: 'POST',
            url: "/bgadmin/API/AdminHandler.ashx",
            data: { TableName: "Books", MethodName: "Add", Entity: JSON.stringify(data.field) },
            success: function (responseData) {
                if (responseData == "true") {
                    parent.layui.table.reload('bookList', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    layer.msg('添加成功');
                    parent.layer.close(index);
                } else {
                    layer.msg('添加失败,请稍后重试');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                layer.msg('添加失败,请稍后重试');
            }
        });
    });

    //Addorder监听提交
    form.on('submit(AddOrder)', function (data) {
        var index = parent.layer.getFrameIndex(window.name);
        $.ajax({
            type: 'POST',
            url: "/bgadmin/API/AdminHandler.ashx",
            data: { TableName: "Orders", MethodName: "Add", Entity: JSON.stringify(data.field) },
            success: function (responseData) {
                if (responseData == "true") {
                    parent.layui.table.reload('orderList', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    parent.layer.close(index);
                    layer.msg('添加成功');
                } else {
                    layer.msg('添加失败,请稍后重试');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                layer.msg('添加失败,请稍后重试');
            }
        });
    });

    //AddUser监听提交
    form.on('submit(AddUser)', function (data) {
        var index = parent.layer.getFrameIndex(window.name);
        $.ajax({
            type: 'POST',
            url: "/bgadmin/API/AdminHandler.ashx",
            data: { TableName: "Users", MethodName: "Add", Entity: JSON.stringify(data.field) },
            success: function (responseData) {
                if (responseData == "true") {
                    parent.layui.table.reload('userList', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    layer.msg('添加成功');
                    parent.layer.close(index);
                } else {
                    layer.msg('添加失败,请稍后重试');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                layer.msg('添加失败,请稍后重试');
            }
        });
    });
});

